﻿using BackgroundTasks.DataModel;
using FantasyWereld.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml.Linq;
using Windows.Data.Xml.Dom;
using Windows.Storage;
using Windows.UI.Notifications;

namespace BackgroundTasks.Code
{
    class Utility
    {
        public static async Task<IEnumerable<RSSChannel>> GetRSSFeedAsync(string url)
        {
            var client = new HttpClient();
            client.MaxResponseContentBufferSize = Constants.MAX_RESPONSE_BUFFERSIZE;

            var response = await client.GetAsync(new Uri(url));
            var stream = await response.Content.ReadAsStreamAsync();

            return getChannelQuery(XDocument.Load(new StreamReader(stream)));
        }

        private static IEnumerable<RSSChannel> getChannelQuery(XDocument xdoc)
        {
            return from channels in xdoc.Descendants("channel")
                   select new RSSChannel
                   {
                       Title = channels.Element("title") != null ? channels.Element("title").Value : "",
                       Link = channels.Element("link") != null ? channels.Element("link").Value : "",
                       Description = channels.Element("description") != null ? channels.Element("description").Value : "",
                       Items = from items in channels.Descendants("item")
                               select new RSSItem
                               {
                                   Title = items.Element("title") != null ? items.Element("title").Value : "",
                                   Link = items.Element("link") != null ? items.Element("link").Value : "",
                                   Description = items.Element("description") != null ? items.Element("description").Value : "",
                                   Guid = (items.Element("guid") != null ? items.Element("guid").Value : "")
                               }
                   };
        }

        public static async Task<XmlDocument> ReadXMLDocumentFromFileAsync(string fileName)
        {
            try
            {
                // Determine source folder location
                StorageFolder sourceFolder = ApplicationData.Current.LocalFolder;

                // Determine source filename
                StorageFile sourceFilename = await sourceFolder.GetFileAsync(fileName);

                XmlLoadSettings loadSettings = new XmlLoadSettings();
                loadSettings.ProhibitDtd = false;
                loadSettings.ResolveExternals = false;

                return await XmlDocument.LoadFromFileAsync(sourceFilename, loadSettings);
            }
            catch
            {
                return null;
            }
        }

        #region SendNotification
        public static void SendNotification(string titel, string message, int type)
        {
            bool showNews = Convert.ToBoolean(ApplicationData.Current.RoamingSettings.Values["ShowPopupNews"]);
            if (showNews)
            {
                string notificationImage = Constants.NOTIFICATION_IMAGE;

                switch (type)
                {
                    case Constants.NOTIFICATION_AUTHORS:
                        notificationImage = Constants.NOTIFICATION_AUTHORS_IMAGE;
                        break;
                    case Constants.NOTIFICATION_BOOKS:
                        notificationImage = Constants.NOTIFICATION_BOOKS_IMAGE;
                        break;
                    case Constants.NOTIFICATION_REVIEWS:
                        notificationImage = Constants.NOTIFICATION_REVIEWS_IMAGE;
                        break;
                    case Constants.NOTIFICATION_STORIES:
                        notificationImage = Constants.NOTIFICATION_STORIES_IMAGE;
                        break;
                }
                var notificationXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText02);
                var toeastElement = notificationXml.GetElementsByTagName("text");
                toeastElement[0].AppendChild(notificationXml.CreateTextNode(titel));
                toeastElement[1].AppendChild(notificationXml.CreateTextNode(message));

                var imageElement = notificationXml.GetElementsByTagName("image");
                imageElement[0].Attributes[1].NodeValue = notificationImage;

                var toastNotification = new ToastNotification(notificationXml);
                ToastNotificationManager.CreateToastNotifier().Show(toastNotification);
            }
        }
        #endregion

    }
}
